"""
Case Type   : fullsql
Case Name   : 参数异常校验--get_global_full_sql_by_timestamp，get_global_slow_sql_by_timestamp
Create At   : 2022/01/29
Owner       : zhao-pengcheng-g
Description :
    1.参数为空报错提示函数不存在
    2.传入一个参数，报错提示函数不存在
    3.传入时间相同的参数,如果statement_history表中存在start_time等于传参的值,返回记录,否则返回空
    4.传入end_time小于begin_time的参数，返回空
    5.传入比集群安装时间小的参数,取交集
    6.传入非时间类型的参数，报错提示函数不存在
    7.传入特殊字符的参数，报错提示函数不存在
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
History     :
"""

import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class Fullsql(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        text = '---Opengauss_Function_Fullsql_Case0018 start---'
        self.logger.info(text)
        self.userNode = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()

    def test_fullsql(self):
        text = '---step1:参数为空，报错提示函数不存在,expect:成功---'
        self.logger.info(text)
        sql_cmd = f'''select * from \
            dbe_perf.get_global_full_sql_by_timestamp();\
            select * from dbe_perf.get_global_slow_sql_by_timestamp();'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertTrue(sql_msg.count(self.constant.NOT_EXIST) == 2,
                        '执行失败:' + text)

        text = '---step2:传入一个参数，报错提示函数不存在,expect:成功---'
        self.logger.info(text)
        sql_cmd = f'''select * from \
                    dbe_perf.get_global_full_sql_by_timestamp\
                    (clock_timestamp());\
                    select * from dbe_perf.get_global_slow_sql_by_timestamp\
                    (clock_timestamp());'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertTrue(sql_msg.count(self.constant.NOT_EXIST) == 2,
                        '执行失败:' + text)

        text = '---step3:传入时间相同的参数,如果statement_history表中' \
               '存在start_time等于传参的值,返回记录,否则返回空,expect:成功---'
        self.logger.info(text)
        sql_cmd = f'''select * from dbe_perf.get_global_full_sql_by_timestamp\
            (clock_timestamp(),clock_timestamp());
            select * from dbe_perf.get_global_slow_sql_by_timestamp\
            (clock_timestamp(),clock_timestamp());'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertTrue(sql_msg.count('(0 rows)') == 2, '执行失败:' + text)

        text = '---step4:传入end_time小于begin_time的参数，返回空,expect:成功---'
        self.logger.info(text)
        sql_cmd = f'''select * from dbe_perf.get_global_full_sql_by_timestamp\
            (clock_timestamp(),clock_timestamp() - 1);
            select * from dbe_perf.get_global_slow_sql_by_timestamp\
            (clock_timestamp(),clock_timestamp() - 1);'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertTrue(sql_msg.count('(0 rows)') == 2, '执行失败:' + text)

        text = '---step5:传入比集群安装时间小的参数,取交集,expect:成功---'
        self.logger.info(text)
        sql_cmd = f'''select * from \
            dbe_perf.get_global_full_sql_by_timestamp\
            (clock_timestamp() - interval '2 years' ,\
            clock_timestamp() - interval '1 year' );
            select * from dbe_perf.get_global_slow_sql_by_timestamp\
            (clock_timestamp() - interval '2 years',\
            clock_timestamp() - interval '1 year');'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertTrue(sql_msg.count('(0 rows)') == 2, '执行失败:' + text)

        text = '---step6:传入非时间类型的参数，报错提示函数不存在,expect:成功---'
        self.logger.info(text)
        sql_cmd = f'''select * from \
            dbe_perf.get_global_full_sql_by_timestamp(100);
            select * from dbe_perf.get_global_slow_sql_by_timestamp(100);'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertTrue(sql_msg.count(self.constant.NOT_EXIST) == 2,
                        '执行失败:' + text)

        text = '---step7:传入特殊字符的参数，报错提示函数不存在,expect:成功---'
        self.logger.info(text)
        sql_cmd = f'''select * from \
            dbe_perf.get_global_full_sql_by_timestamp('&*%^&$^%#');
            select * from \
            dbe_perf.get_global_slow_sql_by_timestamp('!@#$%^&*');'''
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertTrue(sql_msg.count(self.constant.NOT_EXIST) == 2,
                        '执行失败:' + text)

    def tearDown(self):
        self.logger.info(
            '---Opengauss_Function_Fullsql_Case0018 finish---')
